<link rel="stylesheet" href="__PUBLIC__/js/jquery-upload/css/jquery.fileupload.css" />
<link rel="stylesheet" href="__PUBLIC__/js/jquery-upload/css/jquery.fileupload-ui.css" />

<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">添加</h4>
        </div>
        <form style="padding:4%;height:500px;overflow-y:auto;" id="addNewsForm" data-toggle="validator" role="form">
            <div class="form-group">
                <label for="recipient-name" class="control-label">标题:</label>
                <input type="text" class="form-control" name="title" value="{$newsInfo.title}" data-error="新闻标题不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">描述:</label>
                <textarea class="form-control" rows="5" name="description">{$newsInfo.description}</textarea>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">内容:</label>
                <script id="container" name="content" type="text/plain">
                
                </script>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">资讯分类:</label>
                <select id="cateList" name="cate_id">
                    <volist name="cateList" id="cate">
                        <option <if condition="$newsInfo['cate_id'] eq $cate['id']"> selected="selected"</if> value="{$cate.id}" parent="{$cate.pid}"  >{$cate.cate_name}</option>
                    </volist>
                </select>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">新闻封面:</label>
                <img id="userAvatarImg"  <if condition="$newsInfo['pic'] neq '' " >src="{$imageServerUrl}{$newsInfo.pic}" <else/> src="__PUBLIC__/images/avatar.png" </if>  width='150px' />
                <input id="fileupload" type="file" name="files[]" multiple style="display: none;">
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">状态:</label>
                <input type="checkbox" name="status" value="1" />
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">类型:</label>
                <input type="radio" id="radio_0" <if condition="$newsInfo['type'] eq 0"> checked="checked" </if>  value="0" name="type" />
                <label for="radio_0">普通</label>
                <input type="radio" id="radio_1" <if condition="$newsInfo['type'] eq 1"> checked="checked" </if> value="1" name="type" />
                <label for="radio_1">头条</label>

                <input type="radio" id="radio_2" <if condition="$newsInfo['type'] eq 2"> checked="checked" </if>  value="2" name="type" />
                <label for="radio_2">热门</label>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">显示顺序:</label>
                <input type="text" class="form-control" name="listorder" value="{$newsInfo.listorder}">
            </div>
            <input type="hidden" name="pic" id="avatar" value="{$newsInfo.pic}" />
            <input type="hidden" name="id" value="{$newsInfo.id}" />
        </form>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="userSave" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/vendor/jquery.ui.widget.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload-ui.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload-process.js"></script>

<script>
    var ROOTPATH = "__ROOT__";
    var newsContent = '{$newsInfo.content|htmlspecialchars_decode}';
    $(function () {
        $("#addNewsForm").validator();
        $("#cateList").select2tree();
        Ue = UE.getEditor('container');
        
        Ue.ready(function() {
            Ue.setContent(newsContent);
        });
        var switchOption = {
            onText: "开",
            offText: "关",
            onColor: "success",
            offColor: "info",
            state: true,
            onSwitchChange: function (event, state) {
                if (state == true) {
                    $(this).val("1");
                } else {
                    $(this).val("0");
                }
            }
        };
   
        $("input:checkbox[name='status']").bootstrapSwitch(switchOption);

    })

    $('input:radio').iCheck({
        checkboxClass: 'icheckbox_flat-blue',
        radioClass: 'iradio_flat-blue',
        increaseArea: '-10%'
    });

    $("#userAvatarImg").click(function(){
        $('#fileupload').click();
    });

    $('#fileupload').fileupload({
        url: "{$imageServerUrl}/Admin/Upload/index",
        dataType: "json",
        formData: {type: "news",userId: "{$Think.session.userId}"},
        autoUpload: true,
        done: function (e, data) {
            var files = data.result;
            var file = files[0];
            //console.log(file.filePath);
            $("#userAvatarImg").attr("src","{$imageServerUrl}"+file.filePath);
            $("#avatar").val(file.filePath);
        }
    });

    $("#userSave").click(function () {
        var validator = $("#addNewsForm").data("bs.validator");
        validator.validate();
        if(!validator.hasErrors()){
            $.ajax({
                url: "{:U('Admin/News/doAddNews')}",
                type: "post",
                dataType: "json",
                data: $("#addNewsForm").serialize(),
                success: function (data) {
                    if(data.status){
                        $("#commonModal").modal('toggle');
                        common.alert("提示", data.msg, function () {
                            common.reload();
                        });
                    }else{
                        common.alert("提示", data.msg)
                    }
                }
            })
        }
    })

    $('#commonModal').on('hidden.bs.modal', function (e) {
        //console.log(123);
        Ue.destroy();
        Ue = null;
    })
</script>